package com.rjwh.dingdong.client.activity.loginregist;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.UserRegisterCodePresenter;
import com.rjwh.dingdong.client.mvp.view.UserRegisterCodeView;
import com.rjwh.dingdong.client.network.HttpWebService;
import com.rjwh.dingdong.client.network.HttpWebServiceCallBack;
import com.rjwh.dingdong.client.util.ContactPhoneUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 验证手机号
 *
 * @author 王小珂
 * @ClassName UserRegisterCodeActivity
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-3-10 下午4:55:49
 * @history 1.YYYY-MM-DD author: description:
 */
public class UserRegisterCodeActivity extends BaseActivity implements UserRegisterCodeView {

    @BindView(R.id.user_register_code_et)
    EditText codeEt;
    @BindView(R.id.user_register_code_get_code_btn)
    TextView getCodeBtn;
    @BindView(R.id.user_register_code_phone)
    TextView codePhone;
    @BindView(R.id.user_register_code_tips_ll)
    LinearLayout tipsLl;
    @BindView(R.id.user_register_code_next_btn)
    Button nextBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    private TimeCount time;
    private String phoneStr;
    private String codeStr;

    private UserRegisterCodePresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_code_layout);
        ButterKnife.bind(this);
        time = new TimeCount(10 * 6000, 1000);// 构造CountDownTimer对象
        initView();
        presenter = new UserRegisterCodePresenter(this,this);
    }

    private void initView() {
        initToolbarHelper();
        phoneStr = getIntent().getStringExtra("phone");
        codeEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = s.toString();
                CheckView(content);
            }
        });

        getCodeBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!AsStrUtils.isEmpty(phoneStr)) {
                    presenter.doGetCode(phoneStr);
                }
            }
        });
    }

    private void CheckView(final String content) {
        if (AsStrUtils.isEmpty(phoneStr) || AsStrUtils.isEmpty(content)) {
            nextBtn.setBackgroundResource(R.drawable.long_btn_unable);
            nextBtn.setClickable(false);
        } else {
            nextBtn.setBackgroundResource(R.drawable.long_btn);
            nextBtn.setClickable(true);
            nextBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    codeStr = content;
                    presenter.doCheckCode(phoneStr, content);
                }
            });
        }
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle("验证手机号");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    @Override
    public void showLoading() {
//        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
//        dismissLoadDialog();
    }

    @Override
    public void setTimerData() {
        time.start();
    }

    @Override
    public void updataView() {
        tipsLl.setVisibility(View.VISIBLE);
        codePhone.setText(phoneStr);
    }

    @Override
    public void goToNextStep() {
        Intent intent = new Intent(UserRegisterCodeActivity.this, UserRegisterPasswordActivity.class);
        intent.putExtra("phone", phoneStr);
        intent.putExtra("smscode", codeStr);
        startActivity(intent);
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            getCodeBtn.setText("获取验证码");
            getCodeBtn.setClickable(true);
            tipsLl.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            getCodeBtn.setClickable(false);
            getCodeBtn.setText(millisUntilFinished / 1000 + "秒");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
